//
//  HMHomeTableViewController.swift
//  GuoGuo
//
//  Created by 艾小新 on 16/7/29.
//  Copyright © 2016年 xiaoxin. All rights reserved.
//

import UIKit
import SVProgressHUD

class HMHomeTableViewController: HMVisitorViewController {
    // 提供数据的ViewModel
    var statuslistViewModel = HMStatusListViewModel()
    
    // 注册cell
    private let HOMECELL = "HOMECELL"

    override func viewDidLoad() {
        super.viewDidLoad()
        
        if isLogon {
            setupUI()
            loadData()
        }  else {
            // 访客视图 创建视图 传递文字和图片
            (view as! HMVisitorView).setNameAndTitle("visitordiscover_feed_image_house", title: "关注一些人，回这里看看有什么惊喜", isHome: true)
        }
    }
    
    // MARK: -私有类，加载界面和数据
    private func setupUI() {
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch", title: "", target: self, action: #selector(HMHomeTableViewController.cancelLeftBtnClick))
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", title: "", target: self, action: #selector(HMHomeTableViewController.pushRightBtnClick))
        
        // 此时把菊花家进去
        tableView.tableFooterView = pullActivityView
        
        // 注册cell
        resgiterCell()
        
        // 给tableView添加系统自带的刷新控件进行测试
        tableView.addSubview(xxrefreshView)
        xxrefreshView.addTarget(self, action: #selector(loadData), forControlEvents: .ValueChanged)
        
        
    }
    
    // 注册cell
    private func resgiterCell() {
        
        tableView.registerClass(HMHomeCellTableViewCell.self, forCellReuseIdentifier: HOMECELL)
        // 这边进行自动计算
        tableView.estimatedRowHeight = 300
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.separatorStyle = .None
    }

       
    @objc private func cancelLeftBtnClick() {
        
        print("取消按钮")
    }
    @objc private func pushRightBtnClick() {
        
        print("推送下一级控制器按钮")
        // 这么直接推送会卡顿，是因为推送的控制器颜色透明，没有做任何设置
        navigationController?.pushViewController(UIViewController(), animated: true)
    }
    // MARK: -懒加载
    private lazy var pullActivityView : UIActivityIndicatorView = {
       
        let u = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge)
        u.color = UIColor.darkGrayColor()
        return u
        
    }()
    
    private lazy var xxrefreshView: XXRefresh = XXRefresh()
    
    // 加载数据
    @objc func loadData() {
        statuslistViewModel.loadRequest(pullActivityView.isAnimating(), callBack: { (isFinished) in
            if isFinished {
                // 刷新
                self.tableView.reloadData()
            } else {
                SVProgressHUD.showErrorWithStatus("加载失败")
            }
             self.endRefresh()
            
        })
    }
    
    // 结束刷新
    private func endRefresh(){
        pullActivityView.stopAnimating()
        xxrefreshView.stopAnimating()
        
    }
}

// MARK: -数据源方法
extension HMHomeTableViewController {
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.statuslistViewModel.hmstatModel?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(HOMECELL, forIndexPath: indexPath) as! HMHomeCellTableViewCell
        cell.statusViewModel = self.statuslistViewModel.hmstatModel![indexPath.row]
        
        return cell
        
    }
    
    // 这个方法是cell出来的时候
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
        // statuslistViewModel, 如果是倒数第一张，就刷新
        if indexPath.row == statuslistViewModel.hmstatModel!.count - 1 && pullActivityView.isAnimating() == false {
            // 1. 让菊花转转起来 --> 代表进入上拉加载的状态
            pullActivityView.startAnimating()
            // 2. 去加载数据
            loadData()
        }
        
    }
   
    
}
